<template>
  <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width: 100%;">
    <el-col :span="20">
      <span>{{ nodeData.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <!-- 两个内容 -->
        <el-col>{{ nodeData.manager }}</el-col>
        <el-col>
          <!-- 下拉菜单 element -->
          <el-dropdown @command="handleCommand">
            <span>
              操作<i class="el-icon-arrow-down" />
            </span>
            <!-- 下拉菜单 -->
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <template v-if="!nodeData.root">
                <el-dropdown-item command="edit">编辑部分</el-dropdown-item>
                <el-dropdown-item command="del">删除部门</el-dropdown-item>
              </template>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>
<script>
import { reqDelDepartments } from '@/api/departments'
export default {
  name: 'TreeTools',
  props: {
    nodeData: {
      type: Object,
      default: () => ({})
    }
  },
  methods: {
    handleCommand (type) {
      if (type === 'add') {
        this.$emit('on-close', {
          type: 'add',
          isshow: true,
          pid: this.nodeData.id
        })
      } else if (type === 'edit') {
        this.$emit('on-close', {
          type: 'edit',
          isshow: true,
          pid: this.nodeData.id
        })
        // console.log(type)
      } else if (type === 'del') {
        this.$confirm('确认删除吗?', '温馨提示', {
          type: 'warning'
        }).then(async () => {
          const ret = await reqDelDepartments(this.nodeData.id)
          console.log(ret)
          if (ret.code === 10000) {
            this.$message.success('删除成功')
            this.$emit('on-success')
          } else {
            this.$message.error(ret.message)
          }
        }).catch(err => {
          // 点击取消按钮后，err的值为cancel
          if (err !== 'cancel') {
            // 删除失败的错误
            this.$message.error('删除部门失败！')
          }
        })
        // reqDelDepartments(this.nodeData.id)
      }
    }
  }

}
</script>
